package com.fr.web.core.service;

import com.fr.base.CodeUtils;
import com.fr.base.FRContext;
import com.fr.base.Inter;
import com.fr.base.StringUtils;
import com.fr.base.core.json.JSONObject;
import com.fr.base.core.util.TemplateUtils;
import com.fr.privilege.authentication.Authentication;
import com.fr.privilege.authentication.AuthenticationFactory;
import com.fr.privilege.authentication.FreeAuthentication;
import com.fr.privilege.authentication.UsernamePasswordAuthentication;
import com.fr.privilege.authority.Authority;
import com.fr.privilege.manager.PrivilegeManager;
import com.fr.privilege.providers.AuthenticationProvider;
import com.fr.privilege.ui.LoginoutUI;
import com.fr.util.Utils;
import com.fr.web.cache.WebletFactory;
import com.fr.web.core.ShowWorkBookPolicy.Policy4Form;
import com.fr.web.core.WebUtils;
import com.fr.web.platform.PlatformUtils;
import java.io.PrintWriter;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

public class LoginOutService extends NoSessionIDService
{
  private static LoginOutService LOGINOUT_SERVICE = new LoginOutService();

  public static LoginOutService getInstance()
  {
    return LOGINOUT_SERVICE;
  }

  public boolean accept(String paramString)
  {
    return ((paramString.startsWith("auth_")) || (paramString.startsWith("platform_")));
  }

  public void process(HttpServletRequest paramHttpServletRequest, HttpServletResponse paramHttpServletResponse, String paramString)
    throws Exception
  {
    if ("auth_loginui".equals(paramString))
      loginUI(paramHttpServletRequest, paramHttpServletResponse);
    else if ("auth_loginplatformui".equals(paramString))
      loginPlatformUI(paramHttpServletRequest, paramHttpServletResponse);
    else if ("auth_login".equals(paramString))
      login(paramHttpServletRequest, paramHttpServletResponse);
    else if ("auth_logout".equals(paramString))
      logout(paramHttpServletRequest, paramHttpServletResponse);
  }

  private static void loginUI(HttpServletRequest paramHttpServletRequest, HttpServletResponse paramHttpServletResponse)
    throws Exception
  {
    Authentication localAuthentication = AuthenticationFactory.extractAuthentication(paramHttpServletRequest);
    if ((localAuthentication == null) || (!(localAuthentication.isAuthenticated())))
    {
      ReportletDealWith.dealWithReportlet(paramHttpServletRequest, paramHttpServletResponse, WebletFactory.createEmbeddedReportlet("/com/fr/privilege/ui/login.cpt", new ShowWorkBookPolicy.Policy4Form()));
    }
    else
    {
      String str = WebUtils.getHTTPRequestParameter(paramHttpServletRequest, "fr_from");
      LoginoutUI localLoginoutUI = FRContext.getPrivilegeManager().getAuthenticationProvider().getLoginoutUI();
      if (StringUtils.isNotEmpty(str))
      {
        paramHttpServletResponse.sendRedirect(CodeUtils.cjkEncode(str));
        return;
      }
      if (StringUtils.isNotEmpty(localLoginoutUI.getAutoRedirect()))
      {
        paramHttpServletResponse.sendRedirect(TemplateUtils.render(localLoginoutUI.getAutoRedirect(), WebUtils.createTemplateMap(paramHttpServletRequest)));
        return;
      }
      Map localMap = WebUtils.createTemplateMap(paramHttpServletRequest);
      localMap.put("fr_info", localAuthentication.getPrincipal() + " already been registered!");
      TemplateUtils.dealWithTemplate("/com/fr/web/platform/html/info.html", paramHttpServletResponse, localMap);
    }
  }

  private static void loginPlatformUI(HttpServletRequest paramHttpServletRequest, HttpServletResponse paramHttpServletResponse)
    throws Exception
  {
    Authentication localAuthentication = AuthenticationFactory.extractAuthentication(paramHttpServletRequest);
    Map localMap = WebUtils.createTemplateMap(paramHttpServletRequest);
    TemplateUtils.dealWithTemplate("/com/fr/web/platform/html/login.html", paramHttpServletResponse, localMap);
  }

  private static void login(HttpServletRequest paramHttpServletRequest, HttpServletResponse paramHttpServletResponse)
    throws Exception
  {
    Object localObject3;
    PrivilegeManager localPrivilegeManager = FRContext.getPrivilegeManager();
    AuthenticationProvider localAuthenticationProvider = localPrivilegeManager.getAuthenticationProvider();
    PrintWriter localPrintWriter = WebUtils.createPrintWriter(paramHttpServletResponse);
    JSONObject localJSONObject = new JSONObject();
    HttpSession localHttpSession = safeGetSession(paramHttpServletRequest);
    Authority[] arrayOfAuthority1 = PlatformUtils.createSessionAuthorities(paramHttpServletRequest, paramHttpServletResponse);
    Object localObject1 = null;
    if (arrayOfAuthority1.length > 0)
    {
      localObject1 = new FreeAuthentication(arrayOfAuthority1);
    }
    else
    {
      String str4;
      String str1 = WebUtils.getHTTPRequestParameter(paramHttpServletRequest, "form_value");
      str2 = WebUtils.getHTTPRequestParameter(paramHttpServletRequest, "__parameters__");
      str3 = str1;
      if (StringUtils.isEmpty(str3))
      {
        str3 = str2;
        if (StringUtils.isEmpty(str3))
        {
          localObject1 = new FreeAuthentication();
        }
        else
        {
          localObject2 = new JSONObject(str3);
          localObject3 = ((JSONObject)localObject2).getString("fr_username");
          str4 = ((JSONObject)localObject2).getString("fr_password");
          localObject1 = new UsernamePasswordAuthentication((String)localObject3, str4);
        }
      }
      else
      {
        localObject2 = new JSONObject(str3);
        localObject3 = ((JSONObject)localObject2).getString("fr_username");
        str4 = ((JSONObject)localObject2).getString("fr_password");
        localObject1 = new UsernamePasswordAuthentication((String)localObject3, str4);
      }
    }
    try
    {
      localAuthenticationProvider.authenticate((Authentication)localObject1);
    }
    catch (Exception localException)
    {
      if (StringUtils.isNotEmpty(WebUtils.getHTTPRequestParameter(paramHttpServletRequest, "showError")))
      {
        localJSONObject.put("resError", "showError");
        localPrintWriter.print(localJSONObject);
        localPrintWriter.flush();
        localPrintWriter.close();
        return;
      }
      throw localException;
    }
    Authority[] arrayOfAuthority2 = ((Authentication)localObject1).getAuthorities();
    localHttpSession.setAttribute("fr_authentication_key", localObject1);
    localHttpSession.setAttribute("fr_username", ((Authentication)localObject1).getPrincipal());
    localHttpSession.setAttribute("fr_authority", PlatformUtils.obtainAuthority(arrayOfAuthority2));
    String str2 = WebUtils.getHTTPRequestParameter(paramHttpServletRequest, "fr_from");
    String str3 = Utils.objectToString(localHttpSession.getAttribute("fr_error"));
    localHttpSession.removeAttribute("fr_error");
    Object localObject2 = FRContext.getPrivilegeManager().getAuthenticationProvider().getLoginoutUI();
    if (StringUtils.isNotEmpty(str2))
    {
      if (str2.indexOf("op=fr_") != -1)
      {
        localObject3 = paramHttpServletRequest.getRequestURL().toString() + "?op=fr_platform";
        localJSONObject.put("url", localObject3);
        localPrintWriter.print(localJSONObject);
        localPrintWriter.flush();
        localPrintWriter.close();
      }
      else
      {
        localJSONObject.put("url", str2);
        localPrintWriter.print(localJSONObject);
        localPrintWriter.flush();
        localPrintWriter.close();
      }
    }
    else if (StringUtils.isNotEmpty(((LoginoutUI)localObject2).getAutoRedirect()))
    {
      localJSONObject.put("url", ((LoginoutUI)localObject2).getAutoRedirect());
      localPrintWriter.print(localJSONObject);
      paramHttpServletResponse.sendRedirect(TemplateUtils.render(((LoginoutUI)localObject2).getAutoRedirect(), WebUtils.createTemplateMap(paramHttpServletRequest)));
      localPrintWriter.flush();
      localPrintWriter.close();
    }
    else if (StringUtils.isNotBlank(str3))
    {
      localPrintWriter.println(str3);
      localPrintWriter.flush();
      localPrintWriter.close();
    }
    else
    {
      localObject3 = WebUtils.createTemplateMap(paramHttpServletRequest);
      ((Map)localObject3).put("fr_info", ((Authentication)localObject1).getPrincipal() + Inter.getLocText("Login") + Inter.getLocText("Successfully") + "!");
      TemplateUtils.dealWithTemplate("/com/fr/web/platform/html/info.html", paramHttpServletResponse, (Map)localObject3);
    }
  }

  private static void logout(HttpServletRequest paramHttpServletRequest, HttpServletResponse paramHttpServletResponse)
    throws Exception
  {
    HttpSession localHttpSession = safeGetSession(paramHttpServletRequest);
    localHttpSession.removeAttribute("fr_authentication_key");
    localHttpSession.removeAttribute("fr_username");
    localHttpSession.removeAttribute("fr_authority");
    LoginoutUI localLoginoutUI = FRContext.getPrivilegeManager().getAuthenticationProvider().getLoginoutUI();
    localLoginoutUI.redirect2LoginUI(paramHttpServletRequest, paramHttpServletResponse, "");
  }

  private static HttpSession safeGetSession(HttpServletRequest paramHttpServletRequest)
  {
    return paramHttpServletRequest.getSession(true);
  }
}